package com.demo.apparel.mapper.web;

import com.demo.apparel.entity.WxUser;
import com.demo.apparel.vo.web.WebWxUserVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface WebWxUserMapper {

    @Select("""
    <script>
    SELECT * FROM `user`
    WHERE 1=1
    <if test="username != null and username != ''">
      AND username LIKE CONCAT('%', #{username}, '%')
    </if>
    <if test="phone != null and phone != ''">
      AND phone LIKE CONCAT('%', #{phone}, '%')
    </if>
    ORDER BY created_time DESC
    LIMIT #{pageSize} OFFSET #{offset}
    </script>
""")
    List<WxUser> selectUsersPage(@Param("username") String username,
                                    @Param("phone") String phone,
                                    @Param("pageSize") int pageSize,
                                    @Param("offset") int offset);

    @Select("""
    <script>
    SELECT COUNT(*)
    FROM `user`
    WHERE 1=1
    <if test="username != null and username != ''">
      AND username LIKE CONCAT('%', #{username}, '%')
    </if>
    <if test="phone != null and phone != ''">
      AND phone LIKE CONCAT('%', #{phone}, '%')
    </if>
    </script>
""")
    int countUsers(@Param("username") String username,
                   @Param("phone") String phone);


    @Select("SELECT * FROM user")
    List<WxUser> selectAllUsers();

    @Delete("DELETE FROM user WHERE id = #{id}")
    int deleteById(String id);


    @Delete({
            "<script>",
            "DELETE FROM user WHERE id IN ",
            "<foreach collection='idList' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    int deleteBatchIds(@Param("idList") List<String> idList);

    /**
     * 查询所有微信用户
     * @return 用户列表
     */
    @Select("SELECT * FROM user")
    List<WxUser> selectAll();


}
